// 处理图片的函数
import { ApiModulesRoutes,BASE_SCREEN_WIDTH,BASE_SCREEN_HEIGHT } from '@/consts'
// import { ApiUrl } from '@/consts'

export const handleImage = fileId => {
  if (!fileId) return ''
  const baseUrl = window.location.origin
  return `${baseUrl}/test/api${ApiModulesRoutes.BASE}/file/image/preview?fileId=${fileId}`
}

export default {
  handleImage,
}

/**
 * 将px转换成vw
 * @param {Number} currentResolution 当前的分辨率 单位为 px  类型是
 */
export const handleScreenWidthToVw = (currentResolution, isDivided = false) => {
  if (isDivided) {
    return ((currentResolution * (window.document.body.offsetWidth / 2)) / BASE_SCREEN_WIDTH).toFixed(4)
  }
  return ((currentResolution * window.document.body.offsetWidth) / BASE_SCREEN_WIDTH).toFixed(4)
}

/**
 * 将px转换成vh
 * @param {Number} currentResolution 当前的分辨率 单位为 px  类型是
 */
export const handleScreenHeightToVh = currentResolution => {
  return ((currentResolution * window.document.body.offsetHeight) / BASE_SCREEN_HEIGHT).toFixed(4)
}


/**
 * 图片转化为base64
 * @param {String} img Image实例
 * 例： const img = new Image();
 *       img.src = 'xxx';
 *      img.onload = () => {
 *        const base64Img = getBase64Image(img)
 *      }
 */

export const getBase64Image = (img) => {
  const canvas = document.createElement('canvas');
  canvas.width = img.width;
  canvas.height = img.height;
  const ctx = canvas.getContext('2d');
  ctx.drawImage(img, 0, 0, img.width, img.height);
  const ext = img.src.substring(img.src.lastIndexOf('.') + 1).toLowerCase();
  const dataURL = canvas.toDataURL('image/' + ext);
  return dataURL;
}

